package com.liukai.novel_info.presenter.impl;


import com.liukai.novel_base.base.BasePresenter;
import com.liukai.novel_commons.API;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.entities.NovelRack;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_commons.utils.RetrofitManager;
import com.liukai.novel_info.call_back.INovelCallBack;
import com.liukai.novel_info.call_back.INovelRackCallBack;
import com.liukai.novel_info.presenter.INovelPresenter;
import com.liukai.novel_info.presenter.INovelRackPresenter;

import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class NovelRackPresenterImpl extends BasePresenter<INovelRackCallBack> implements INovelRackPresenter {


    @Override
    public void updateRackById(NovelRack novelRack) {
        Retrofit retrofit = RetrofitManager.getRackInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<Result> resultCall = api.updateRackById(novelRack);
        resultCall.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                LogUtils.e(getClass(), "response:" + response.toString());
//                mCallBack.updateRackByIdResult();
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                LogUtils.e(getClass(), "Throwable:" + t.toString());

            }
        });
    }

    @Override
    public void findChapterNameById(Integer chapterId, String novelId) {
        Retrofit retrofit = RetrofitManager.getNovelInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<Result> chapterNameById = api.findChapterNameById(chapterId, novelId);
        chapterNameById.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                LogUtils.e(getClass(), "response:" + response.toString());
                mCallBack.findChapterNameByIdResult(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

    @Override
    public void addRack(NovelRack novelRack) {
        Retrofit retrofit = RetrofitManager.getRackInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<Result> addRackTask = api.addRack(novelRack);
        addRackTask.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                LogUtils.e(getClass(), response.body().toString());
                LogUtils.e(getClass(), response.toString());
                mCallBack.addRackResult(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                LogUtils.e(getClass(), t.toString());
            }
        });
    }

}
